#!/usr/bin/env python
#coding=utf-8

import yaml
import logging
from pprint import pprint
from kubernetes import client
from core.logger import logger

class K8sStatefulset(object):

    def __init__(self):
        self.apps_v1 = client.AppsV1Api()


    def create_namespaced_statefulset(self, statefulset_file, namespace):
        try:
            with open(statefulset_file) as f:
                dep = yaml.safe_load(f)

            resp = self.apps_v1.create_namespaced_stateful_set(body=dep, namespace=namespace)
        except Exception as e:
            logger.error(f"created statefulset:{statefulset_file} error=>{e}")
            return False
        logger.debug(f"created statefulset:{resp.metadata.name} success")



    def delete_namespaced_statefulset(self, statefulset_name, namespace):
        try:
            resp = self.apps_v1.delete_namespaced_stateful_set(name=statefulset_name, namespace=namespace)
        except Exception as e:
            logger.error(f"delete statefulset:{statefulset_name} error=>{e}")
            return False

        logger.debug(f"delete statefulset:{statefulset_name} success")
        return True



    def patch_namespaced_statefulset(self, statefulset_name, namespace, replicas_number):
        dep = {
            "spec": {
                "replicas": replicas_number
            }
        }
        try:
            resp = self.apps_v1.patch_namespaced_stateful_set(name=statefulset_name, body=dep, namespace=namespace)
        except Exception as e:
            logger.error(f"patch statefulset:{statefulset_name} replicas_number:{replicas_number} error=>{e}")
            return None

        logger.debug(f"patch statefulset:{statefulset_name} replicas_number:{replicas_number} success")
        return resp



    def read_namespaced_statefulset(self, statefulset_name, namespace):
        try:
            resp = self.apps_v1.read_namespaced_stateful_set(name=statefulset_name, namespace=namespace)
            data = {
                "name": resp.metadata.name,
                "replicas": resp.spec.replicas,
                "ready_replicas": resp.status.ready_replicas,
                "unavailable_replicas": resp.status.unavailable_replicas,
            }
        except Exception as e:
            logger.error(f"read statefulset:{statefulset_name} namespace:{namespace} error=>{e}")
            return None

        logger.debug(f"read statefulset:{statefulset_name} namespace:{namespace} success")
        return data



    def list_namespaced_statefulset(self, namespace_name):
        statefulsets_info = []
        continue_flag = ""
        while True:
            try:
                resp = self.apps_v1.list_namespaced_stateful_set(namespace=namespace_name, _continue=continue_flag)
            except Exception as e:
                logger.error("list_namespaced_statefulset error=>{}".format(e))
                return None
            statefulsets_info.extend(resp.items)

            continue_flag = resp.metadata._continue
            if not continue_flag:
                break

        return statefulsets_info
